package com.ita.acmsystem.merchant.listener;

import java.io.IOException;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.acmsystem.merchant.dao.IMerchantDao;
import com.ita.acmsystem.merchant.dao.impl.MerchantDaoImpl;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.util.JsonUtil;

public class ConsumerMsgListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		ObjectMapper mapper = new ObjectMapper();
		TextMessage tm = (TextMessage) message;
		IMerchantDao dao = new MerchantDaoImpl();
			try {
				System.out.println("[JMS] Receive Message: " + tm.getText());
				Map<String, String> map = mapper.readValue(tm.getText(), Map.class);
				String id = map.get("merchantId");
				String isBlack = map.get("isBlack");
				System.out.println(id + "...JMS receive..." + isBlack);
				Merchant merchant = dao.queryById(id);
				if (merchant != null) {
					merchant.setMerchantBlanklist(Integer.valueOf(isBlack));
					dao.update(merchant);
				}
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
	}

}
